package com.bootdo.imgtext.service.impl;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import com.bootdo.imgtext.check.CheckFactory;
import com.bootdo.imgtext.check.CheckService;
import com.bootdo.imgtext.dao.DocfileDao;
import com.bootdo.imgtext.dao.DocitemDao;
import com.bootdo.imgtext.domain.DocfileDO;
import com.bootdo.imgtext.domain.DocitemDO;
import com.bootdo.imgtext.service.DocfileService;

@Service
public class DocfileServiceImpl implements DocfileService {

	private static final Logger log = LoggerFactory.getLogger(DocfileServiceImpl.class);

	@Autowired
	private DocfileDao docfileDao;

	@Autowired
	private DocitemDao docitemDao;

	@Override
	public DocfileDO get(Integer id) {
		return docfileDao.get(id);
	}

	@Override
	public List<DocfileDO> list(Map<String, Object> map) {
		return docfileDao.list(map);
	}

	@Override
	public int count(Map<String, Object> map) {
		return docfileDao.count(map);
	}

	@Override
	public int save(DocfileDO docfile) {
		return docfileDao.save(docfile);
	}

	@Override
	public int update(DocfileDO docfile) {
		return docfileDao.update(docfile);
	}

	@Override
	public int remove(Integer id) {
		return docfileDao.remove(id);
	}

	@Override
	public int batchRemove(Integer[] ids) {
		return docfileDao.batchRemove(ids);
	}

	@Override
	@Async
	public void saveAndCheck(int id, List<List<String>> items) {
		int sum = items.stream().parallel().map(list -> getItem(list, id)).mapToInt(this::check).sum();

		DocfileDO docfileDO = new DocfileDO();
		docfileDO.setId(id);
		docfileDO.setStatus("检测完毕");
		docfileDO.setAcceptanceNum(sum);
		docfileDO.setFailNum(items.size() - sum);
		docfileDao.update(docfileDO);
	}

	private DocitemDO getItem(List<String> list, int fileId) {
		DocitemDO docitemDO = new DocitemDO();
		docitemDO.setSn(list.get(0));
		docitemDO.setDocDate(list.get(1));
		docitemDO.setTitle(list.get(2));
		docitemDO.setUrl(list.get(3));
		docitemDO.setViews(list.get(4));
		docitemDO.setItemComments(list.get(5));
		docitemDO.setCheckResult("准备检测");
		docitemDO.setFileId(fileId);
		log.debug("docitemDO=>{}", docitemDO);
		docitemDao.save(docitemDO);
		return docitemDO;
	}

	private int check(DocitemDO docitemDO) {
		try {
			CheckService result = CheckFactory.getCheckService(docitemDO.getUrl());
			if (result == null) {
				updateById(docitemDO.getId(), "暂不支持该网站");
				return 0;
			}
			if (result.getException() != null) {
				updateById(docitemDO.getId(), result.getException());
				return 0;
			}

			StringBuilder sb = new StringBuilder();
			sb.append(result.hasLogo() ? "有" : "无").append("logo图片; ");
			if (result.author() != null && result.author().length() > 0) {
				sb.append(result.author()).append(";");
			} else {
				sb.append("无作者; ");
			}
			sb.append("图片数量：").append(result.imageNum()).append("; ");
			sb.append("含logo图片数量：").append(result.logoImageNum()).append("; ");
			String res = sb.toString();

			updateById(docitemDO.getId(), res);

			if (res.contains("无logo图片;") || res.contains("无作者")) {
				return 0;
			}
			return 1;
		} catch (Exception e) {
			updateById(docitemDO.getId(), e.toString());
			log.error(e.getMessage(), e);
			return 0;
		}

	}

	private void updateById(int id, String message) {
		DocitemDO updateItem = new DocitemDO();
		updateItem.setId(id);
		if (message == null) {
			updateItem.setCheckResult("连接失败");
		} else {
			updateItem.setCheckResult(message.length() > 100 ? message.substring(0, 100) : message);
		}
		docitemDao.update(updateItem);
	}

}
